package com.dq.domain.sys.user.user;

import com.alibaba.fastjson.JSONObject;
import com.dq.vo.baseVo.VO;

import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.user.user.base.BackUser;
import com.dq.utils.TimeUtil;
import com.google.common.base.Joiner;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author liuhui
 * @date 2019/11/22 0022 11:10:02
 */
@Data
@Entity
@ApiModel("门店用户")
public class OrgUser extends BackUser implements VO.ToVO {
    @Data
    @ApiModel(value = "用户显示结构")
    public static class Vo implements VO {
        @ApiModelProperty(value = "用户id")
        private Long id;
        @ApiModelProperty(value = "用户编号")
        private String cardNo;
        @ApiModelProperty(value = "用户登陆名")
        private String username;
        @ApiModelProperty(value = "邮箱")
        private String email;
        @ApiModelProperty(value = "手机号")
        private String mobile;
        @ApiModelProperty(value = "状态")
        private String status;
        @ApiModelProperty("状态")
        protected String statusEn;
        @ApiModelProperty("状态")
        protected String statusCn;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "头像")
        private String headImg;
        @ApiModelProperty(value = "邀请人微信手机号")
        private String inviteUserMobile;
        @ApiModelProperty("token")
        private String token;
        @ApiModelProperty("绑定门店")
        private Long orgId;
        @ApiModelProperty("门店名称")
        private String orgName;
        @ApiModelProperty(value = "组织部门/门店")
        private Long sectionId;
        @ApiModelProperty(value = "组织部门/门店")
        private String sectionSimpleName;

        @ApiModelProperty("省份")
        protected String province;
        @ApiModelProperty("城市")
        protected String city;
        @ApiModelProperty("区")
        protected String district;

        @ApiModelProperty("昵称")
        private String localNickName;
        @ApiModelProperty("门店信息")
        private Org.Vo orgVo;
        private String userRealcName;
        @ApiModelProperty(value = "角色code列表")
        private Set<String> roleCodes;
        @ApiModelProperty("菜单id列表")
        private List<SysMenu.Vo> menuList;
        @ApiModelProperty("按钮id列表")
        private Set<SysMenu.Vo> buttonList;

        protected String postIds;
        protected String roleIds;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.cardNo = this.cardNo;
        vo.username = this.username;
        vo.email = this.email;
        vo.mobile = this.mobile;
        vo.status = this.status.name;
        vo.statusEn = fmt(this.getStatus().toString());
        vo.statusCn = fmt(this.getStatus().name);

        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.headImg = this.headImg;
        vo.orgId = this.getSection() == null ? null : this.getSection().getId();
        vo.localNickName = this.getUserRealcName();
        vo.userRealcName = this.getUserRealcName();

        vo.province = fmt(this.getProvince());
        vo.city = fmt(this.getCity());
        vo.district = fmt(this.getDistrict());

        vo.sectionId = this.getSection() == null ? null : this.getSection().getId();
        vo.sectionSimpleName = this.getSection() == null ? "" : this.getSection().getSimpleName();

        List<Long> longs = new ArrayList<>();
        OrgPosition orgPosition = this.getOrgPosition();
        Long postId = null;
        if(orgPosition != null){
            postId = orgPosition.getId();
            longs.add(postId);
        }
        vo.postIds = orgPosition!= null? Joiner.on(",").join(longs):"";
        vo.roleIds = this.roleIds;
        return vo;
    }

    public JSONObject toBasicVO() {
        JSONObject vo = new JSONObject();
        vo.put("id", this.getId());
        vo.put("cardNo", this.getCardNo());
        vo.put("userRealcName", this.getUserRealcName());
        vo.put("username", this.getUsername());
        vo.put("mobile", this.getMobile());
        vo.put("statusEn", this.getStatus().toString());
        vo.put("statusCn", this.getStatus().name);
        vo.put("headImg", this.getHeadImg());
        vo.put("createTime", fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS));

        if(this.getSection() != null) {
            Org org = (Org) this.getSection();
            vo.put("orgId", this.getSection() == null ? null : org.getId());
            vo.put("orgName", this.getSection() == null ? null : org.getSimpleName());
            vo.put("legalName", this.getSection() == null ? null : org.getLegalName());
        }

        return vo;
    }
}
